with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Text_IO; use Ada.Text_IO;
with ariane.elemental.autoptr;
use ariane.elemental;

procedure test_elemental_autoptr is
  type myrec_t is tagged record
    id    : integer;
    name  : string(1..10);
  end record;

  id : integer := 1;

  procedure myrecinit(myrec : in out myrec_t) is
  begin
    myrec.id := id;
    id := id + 1;
    myrec.name := 10 * ' ';
    myrec.name := overwrite(myrec.name, 1, "rec");
    myrec.name := overwrite(myrec.name, 4, trim(integer'Image(id), Both));
    put("record{");
    put(integer'Image(myrec.id)); put("; '"); put(myrec.name);
    put_line("'} created");
  end myrecinit;

  package myptr is new autoptr(target_t => myrec_t,
                               target_initialize => myrecinit'Access);

  p1, p2 : myptr.Pointer;

begin
  p1 := myptr.create;
  put("reference count of p1 is "); put_line(integer'Image(p1.numrefs));

  p2 := p1;
  put("reference count of p2 is "); put_line(integer'Image(p2.numrefs));

  put("p2.name = '"); put(p2.target.name); put_line("'");

end test_elemental_autoptr;

